﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class Forget : System.Web.UI.PageEx
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitCaptchaCode();
            string strLink = "<li><a href=\"{0}\" class=\"{2}\">{1}</a></li>";
            StringBuilder sbMenuLinks = new StringBuilder();
            if (UserType == 1 && UserId > 0)
            {
                sbMenuLinks.Append(string.Format(strLink, new object[] { "MemberCenter.aspx", "会员中心", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "UserInfos.aspx", "我的资料", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "ModifyPwd.aspx", "修改密码", "on" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "MyBaoMing.aspx", "我的报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "MyYiGongBaoMing.aspx", "义工报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "MyHomeEventBaoMing.aspx", "家园活动报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "Logout.aspx", "退出系统", "" }));
            }
            else
            {
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "会员中心", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "我的资料", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "修改密码", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "我的报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "义工报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "家园活动报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "退出系统", "" }));
            }
            litMenuLinks.Text = sbMenuLinks.ToString();
        }
    }
    protected void btnGetPwd_Click(object sender, EventArgs e)
    {
        if (txtVaildCode.Text != Session["CaptchaImageText"].ToString())
        {
            MessageBox("验证码错误！");
            return;
        }
        string strUserName = SqlHelper.String(txtUserName.Text);
        string strEmail = SqlHelper.String(txtEmail.Text);
        DataSet ds = DalEx.DbUserDalEx.Select(" and UserName='" + strUserName + "' and Email='" + strEmail + "'");
        if (SqlHelper.HasRow(ds))
        {
            Model.DbUsers dbUsers = new Model.DbUsers(ds.Tables[0].Rows[0]);
            string strResult = string.Empty;
            if (DalEx.DbUserDalEx.GetForgetPwd(dbUsers, ref strResult))
            {
                MessageBox(strResult, "UserLogin.aspx");
            }
            else
            {
                MessageBox(strResult);
                return;
            }
        }
        else
        {
            MessageBox("抱歉，您的用户名不存在或您注册时填写的电子邮件不符！！");
            return;
        }
    }

    #region 验证码
    private void LoadData()
    {
        InitCaptchaCode();
    }
    /// <summary>
    /// 初始化验证码
    /// </summary>
    private void InitCaptchaCode()
    {
        // 创建一个 6 位的随机数并保存在 Session 对象中
        HttpContext.Current.Session["CaptchaImageText"] = GenerateRandomCode();
        imgVaildCode.ImageUrl = "~/Basic/VaildCodeImage.ashx?w=120&h=30&t=" + DateTime.Now.Ticks;
    }
    /// <summary>
    /// 创建一个 6 位的随机数
    /// </summary>
    /// <returns></returns>
    private string GenerateRandomCode()
    {
        string s = String.Empty;
        Random random = new Random();
        for (int i = 0; i < 6; i++)
        {
            s += random.Next(10).ToString();
        }
        return s;
    }
    /// <summary>
    /// 重新加载验证码
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        InitCaptchaCode();
    }
    #endregion
}
